package com.zz.tijian.handle.mq;

import org.springframework.amqp.core.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class RabbitMQConfig {

    /*
    退款处理队列定义
     */
    public static final String REFUND_QUEUE = "refundQueue";
    public static final String REFUND_EXCHANGE = "refundExchange";
    public static final String REFUND_ROUTING_KEY = "refundRoutingKey";

    public static final String DLX_EXCHANGE = "dlxExchange";
    public static final String DLX_QUEUE = "dlxQueue";
    public static final String DLX_ROUTING_KEY = "dlxRoutingKey";
    /*
     支付处理队列定义
     */
    public static final String PAY_QUEUE = "payQueue";
    public static final String PAY_EXCHANGE = "payExchange";
    public static final String PAY_ROUTING_KEY = "payRoutingKey";


    @Bean
    public Queue refundQueue() {
        return QueueBuilder.durable(REFUND_QUEUE)
                .withArgument("x-dead-letter-exchange", DLX_EXCHANGE)
                .withArgument("x-dead-letter-routing-key", DLX_ROUTING_KEY)
                .build();
    }

    @Bean
    public TopicExchange refundExchange() {
        return new TopicExchange(REFUND_EXCHANGE);
    }

    @Bean
    public Binding refundBinding(Queue refundQueue, TopicExchange refundExchange) {
        return BindingBuilder.bind(refundQueue).to(refundExchange).with(REFUND_ROUTING_KEY);
    }

    @Bean
    public Queue dlxQueue() {
        return QueueBuilder.durable(DLX_QUEUE).build();
    }

    @Bean
    public TopicExchange dlxExchange() {
        return ExchangeBuilder
                .topicExchange(DLX_EXCHANGE)
                .durable(true)
                .build();
    }

    @Bean
    public Binding dlxBinding(Queue dlxQueue, TopicExchange dlxExchange) {
        return BindingBuilder.bind(dlxQueue).to(dlxExchange).with(DLX_ROUTING_KEY);
    }

    /*
     * 支付处理队列定义
     */
    @Bean
    public Queue payQueue() {
        return QueueBuilder.durable(PAY_QUEUE).build();
    }

    @Bean
    public TopicExchange payExchange() {
        return new TopicExchange(PAY_EXCHANGE);
    }

    @Bean
    public Binding payBinding(Queue payQueue, TopicExchange payExchange) {
        return BindingBuilder.bind(payQueue).to(payExchange).with(PAY_ROUTING_KEY);
    }



    /*
    订单支付超时队列定义
    */
    public static final String ORDER_PAY_QUEUE = "orderPayQueue";
    public static final String ORDER_PAY_EXCHANGE = "orderPayExchange";
    public static final String ORDER_PAY_ROUTING_KEY = "orderPayRoutingKey";

    /*
    订单超时处理队列定义
    */
    public static final String ORDER_TIMEOUT_QUEUE = "orderTimeoutQueue";
    public static final String ORDER_TIMEOUT_EXCHANGE = "orderTimeoutExchange";
    public static final String ORDER_TIMEOUT_ROUTING_KEY = "orderTimeoutRoutingKey";

    @Bean
    public Queue orderPayQueue() {
        return QueueBuilder.durable(ORDER_PAY_QUEUE)
                .build();
    }

    @Bean
    public DirectExchange orderPayExchange() {
        return new DirectExchange(ORDER_PAY_EXCHANGE);
    }

    @Bean
    public Binding orderPayBinding() {
        return BindingBuilder.bind(orderPayQueue()).to(orderPayExchange()).with(ORDER_PAY_ROUTING_KEY);
    }

    @Bean
    public Queue orderTimeoutQueue() {
        //TODO 抽出延迟时间设置
        return QueueBuilder.durable(ORDER_TIMEOUT_QUEUE)
                .withArgument("x-message-ttl", 60000) // 设置消息的 TTL，这里是 60 秒
                .withArgument("x-dead-letter-exchange", ORDER_PAY_EXCHANGE) // 指定死信交换机为支付交换机
                .withArgument("x-dead-letter-routing-key", ORDER_PAY_ROUTING_KEY) // 指定死信路由键为支付路由键
                .build();
    }

    @Bean
    public DirectExchange orderTimeoutExchange() {
        return new DirectExchange(ORDER_TIMEOUT_EXCHANGE);
    }

    @Bean
    public Binding orderTimeoutBinding() {
        return BindingBuilder.bind(orderTimeoutQueue()).to(orderTimeoutExchange()).with(ORDER_TIMEOUT_ROUTING_KEY);
    }



}
